package com.linkoog.devtools.code_generator.setting.configurable.ui.template;

import com.linkoog.devtools.code_generator.template.model.node.DirNodeData;
import com.linkoog.devtools.code_generator.template.model.node.TemplateNodeData;
import com.linkoog.devtools.code_generator.template.model.node.FileNodeData;
import com.linkoog.devtools.code_generator.util.FileTypeUtil;
import com.intellij.icons.AllIcons;
import com.intellij.icons.AllIcons.Nodes;
import com.intellij.openapi.fileTypes.FileType;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;

public class TemplateTreeCellRenderer extends DefaultTreeCellRenderer {


    private static final long serialVersionUID = -6564861041507376828L;

    /**
     * 重写父类DefaultTreeCellRenderer的方法
     */
    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean isLeaf, int row, boolean hasFocus) {
        // 选中
        if (selected) {
            setForeground(getTextSelectionColor());
        } else {
            setForeground(getTextNonSelectionColor());
        }


        // TreeNode
        DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) value;
        Object obj = treeNode.getUserObject();
        if (obj instanceof TemplateNodeData) {
            TemplateNodeData rootNode = (TemplateNodeData) obj;
            DefaultTreeCellRenderer tempCellRenderer = new DefaultTreeCellRenderer();
            tempCellRenderer.setOpenIcon(AllIcons.Nodes.JavaModule);
            tempCellRenderer.setClosedIcon(AllIcons.Nodes.JavaModule);
            tempCellRenderer.setLeafIcon(AllIcons.Nodes.JavaModule);
            return tempCellRenderer.getTreeCellRendererComponent(tree, rootNode.getName(), selected, expanded, false, row, hasFocus);
        }
        else if (obj instanceof DirNodeData) {
            DirNodeData packageNode = (DirNodeData) obj;
            DefaultTreeCellRenderer tempCellRenderer = new DefaultTreeCellRenderer();
            tempCellRenderer.setOpenIcon(Nodes.Package);
            tempCellRenderer.setClosedIcon(Nodes.Package);
            tempCellRenderer.setLeafIcon(Nodes.Package);
            return tempCellRenderer.getTreeCellRendererComponent(tree, packageNode.getName(), selected, expanded, false, row, hasFocus);
        }
        else if (obj instanceof FileNodeData) {
            FileNodeData fileNode = (FileNodeData) obj;
            DefaultTreeCellRenderer tempCellRenderer = new DefaultTreeCellRenderer();
            FileType fileType = FileTypeUtil.getFileType(fileNode.getType());
            tempCellRenderer.setOpenIcon(fileType.getIcon());
            tempCellRenderer.setClosedIcon(fileType.getIcon());
            tempCellRenderer.setLeafIcon(fileType.getIcon());
            return tempCellRenderer.getTreeCellRendererComponent(tree, fileNode.getName(), selected, expanded, true, row, hasFocus);
        } else {
            String text = (String) obj;
            DefaultTreeCellRenderer tempCellRenderer = new DefaultTreeCellRenderer();
            return tempCellRenderer.getTreeCellRendererComponent(tree, text, selected, expanded, false, row, hasFocus);
        }
    }


}
